package org.example.service.impl;


import org.example.mapper.UserMapper;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.utils.Md5Util;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }

    @Override
    public void SignIn(String username, String password, String email, String nickname) {

        String md5password = Md5Util.getMD5String(password);

        userMapper.SignIn(username, md5password, email, nickname);
    }

    @Override
    public void addCoupleName(String nickname) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Object id = map.get("id");

        userMapper.addCoupleName(nickname, id);
    }

    @Override
    public void upload(String url) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Object id = map.get("id");

        userMapper.upload(url, id);
    }

    //查询昵称
    @Override
    public User findByNickname(String nickname) {
        return userMapper.findByNickname(nickname);
    }

    @Override
    public User findByUser(Integer id) {
        return userMapper.findByUser(id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String Md5rePwd = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(id, Md5rePwd);
    }

    @Override
    public String findByEamil(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public void updatePwdEmail(String email, String password) {
        String Md5rePwd = Md5Util.getMD5String(password);
        userMapper.updatePwdEmail(email, Md5rePwd);
    }

}
